package day2_linkedlist.leetcode;

import java.util.LinkedList;
import java.util.List;

/**
 * @author yuisama
 * @date 2022/11/15 14:54
 * 剑指offer06_逆向输出链表
 **/
public class Offer06_ReverseList {
    List<Integer> list = new LinkedList<>();
    public int[] reversePrint(ListNode head) {
        traverse(head);
        int[] ret = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ret[i] = list.get(i);
        }
        return ret;
    }

    /**
     * 反向遍历以head为头的单链表
     * @param head
     */
    private void traverse(ListNode head) {
        // 1.base case
        if (head == null) {
            return;
        }
        // 先序位置
        traverse(head.next);
        // 后序位置
        list.add(head.val);
    }
}